// 引入Vue
import Vue from 'vue'
//引入VueRouter
import VueRouter from 'vue-router'
// 引入一堆路由组件
import Home from '@/pages/Home'
import Login from '@/pages/Login'
import Register from '@/pages/Register'
import Search from '@/pages/Search'
import Test from '@/pages/Test'

//应用VueRouter
Vue.use(VueRouter)

//第一步：将VueRouter原型对象上的push缓存一份
const originPush = VueRouter.prototype.push
//第一步：将VueRouter原型对象上的replace缓存一份
const originReplace = VueRouter.prototype.replace

//第二步：增强push
VueRouter.prototype.push = function(location,okCallback,errCallback){
	//如果程序员调用this.$router.push的时候，只传了跳转的位置
	if(okCallback === undefined && errCallback === undefined){
		return originPush.call(this,location).catch(()=>{})
	}else{
		return originPush.call(this,location,okCallback,errCallback)
	}
}
//第二步：增强replace
VueRouter.prototype.replace = function(location,okCallback,errCallback){
	//如果程序员调用this.$router.push的时候，只传了跳转的位置
	if(okCallback === undefined && errCallback === undefined){
		return originReplace.call(this,location).catch(()=>{})
	}else{
		return originReplace.call(this,location,okCallback,errCallback)
	}
}

// 创建一个路由器
export default new VueRouter({
	mode:'history',
	routes:[
		{
			name:'home',
			path:'/home',
			component:Home,
			meta:{showFooter:true}
		},
		{
			name:'login',
			path:'/login',
			component:Login,
			meta:{showFooter:false}
		},
		{
			name:'register',
			path:'/register',
			component:Register,
			meta:{showFooter:false}
		},
		{
			name:'search',
			path:'/search',
			component:Search,
			meta:{showFooter:true}
		},
		{
			name:'test',
			path:'/test',
			component:Test,
			meta:{showFooter:true}
		},
		{
			path:'/',
			redirect:'/home'
		}
	]
})


